<?php

Raise::load('core.RaiseObject');
Raise::load('core.RaiseSession');

/**
 * RaiseUserFactory class
 * Manages storage of user identity in the session
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.Authentication
 * @since 1.2
 */
class RaiseUserFactory extends RaiseObject {

    /**
     * The session key that will contain the user information
     */
    const SESSION_KEY = '_?:RaiseUserFactory';

    /**
     * The session in which this user
     * @var RaiseSession
     */
    private $session;

    /**
     * Create a new RaiseUserFactory depending on a session
     * @param RaiseSession $session
     */
    public function __construct($session){
        $this->session = $session;
    }

    /**
     * Get or set the current user in the session.
     * @param mixed $user (optional) The new user to set to.
     * @return mixed
     */
    public function user($user = null){
        if(func_num_args() == 1){
            $this->session->set(self::SESSION_KEY, $user);
        }

        $user = $this->session->get(self::SESSION_KEY);
        return $user;
    }

}
